package com.bq.gametrade.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bq.gametrade.HttpLink;
import com.bq.gametrade.R;
import com.bq.gametrade.base.BaseActivity;
import com.bq.gametrade.bean.ResponseBean;
import com.bq.gametrade.bean.UserInfoDetails;
import com.bq.gametrade.http.VRHttp;
import com.bq.gametrade.http.VRHttpListener;
import com.bq.gametrade.utils.Base64Util;
import com.bq.gametrade.utils.NetworkUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class FriendAddActivity extends BaseActivity implements View.OnClickListener{

    private TextView title;
    private ImageView back;
    private EditText etUserAccount;
    private Button btnAdd;
    private UserInfoDetails userInfo;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_friendadd);
        if(application.isLogin()){
            userInfo = application.getUserInfo();
        }else {
            startActivity(new Intent(this, LoginActivity.class));
            finish();
        }
        initView();
    }

    private void initView(){
        title = serchViewById(R.id.title_name);
        title.setText("新朋友");
        back = serchViewById(R.id.image_back);
        back.setOnClickListener(this);
        etUserAccount = serchViewById(R.id.et_userAccount);
        btnAdd = serchViewById(R.id.btn_add);
        btnAdd.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.image_back:
                finish();
                break;
            case R.id.btn_add:
                if(TextUtils.isEmpty(etUserAccount.getText().toString())){
                    toastShow("请输入好友手机号或者比奇账号");
                    return;
                }
                if (etUserAccount.getText().toString().equals(application.getUserInfo().account)){
                    toastShow("不能添加自己为好友");
                    return;
                }

                if(!NetworkUtils.isNetworkAvailable(FriendAddActivity.this)){
                   toastShow("网络异常");
                    return;
                }
                addFriend();
                break;
        }
    }

    private void addFriend(){
        Map<String,String> params = new HashMap();
        params.put("token",userInfo.token);
        params.put("friendId",etUserAccount.getText().toString());
        params.put("remark", "");
        showSpotsDialog("正在添加····");
        VRHttp.getResponse(FriendAddActivity.this, HttpLink.ADD_FRIENDS_URL, params, new VRHttpListener() {
            @Override
            public void onSuccess(Object response, boolean isCache) {
               if(response != null){
                   try {
                       JSONObject object = new JSONObject(response.toString());
                       String status = object.getString("status");
                       String message = object.getString("message");
                       if(status.equals("0")){
                           toastShow(Base64Util.decodeStr(message));
                           finish();
                       }else if(status.equals("1")){
                           toastShow(Base64Util.decodeStr(message));
                       }else if (status.equals("99")) {
                           toastShow("登录超时请重新登陆");
                           application.setUserInfo(null);
                           startActivity(new Intent(FriendAddActivity.this, LoginActivity.class));
                       } else if (status.equals("98")) {
                           toastShow("您的账号已在其它设备登录，请重新登录修改您的密码");
                           application.setUserInfo(null);
                           startActivity(new Intent(FriendAddActivity.this, LoginActivity.class));
                           finish();
                       } else {
                           toastShow("添加好友失败！");
                       }
                   }catch (JSONException e){
                       e.printStackTrace();
                   }
               }
            }

            @Override
            public void onFinish() {
                super.onFinish();
                closeSpotsDialog();
            }
        });
    }
}
